package com.lipiao.nb_iot;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.ashokvarma.bottomnavigation.TextBadgeItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener, ViewPager.OnPageChangeListener{
    BottomNavigationBar mBottomNavigationBar;
    TextBadgeItem mBadgeItem;
    TextBadgeItem mBadgeItem2;
    TextBadgeItem mBadgeItem3;

    static ViewPager viewPager;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

        private void initView() {
        initBottomNavigationBar();//初始化底部工具栏
        initViewPager();//初始化Viewpager
    }

    private void initViewPager() {
        viewPager = findViewById(R.id.view_pager);

        fragments = new ArrayList<Fragment>();
        fragments.add(new StatusFragment());
        fragments.add(new WarnFragment());
        fragments.add(new UserFragment());

        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);
    }

    private void initBottomNavigationBar() {
        mBottomNavigationBar = findViewById(R.id.bottom_navigation_bar);
        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.icons8_status_48px, "状态")
                        .setActiveColorResource(R.color.colorChooseTab)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem))
                .addItem(new BottomNavigationItem(R.drawable.icons8_warning_shield_48px, "报警")
                        .setActiveColorResource(R.color.colorChooseTab)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem2))
                .addItem(new BottomNavigationItem(R.drawable.icons8_user_48px, "用户")
                        .setActiveColorResource(R.color.colorChooseTab)
                        .setInActiveColor(R.color.colorTab)
                        .setBadgeItem(mBadgeItem3))//依次添加item,分别icon和名称
                .setFirstSelectedPosition(0)//设置默认选择item
                .setBarBackgroundColor(R.color.colorPrimaryDark)
                .setTabSelectedListener(this)
                .initialise();//初始化
    }

    //BottomNavigationBar.OnTabSelectedListener接口重载
    @Override
    public void onTabSelected(int position) {
        viewPager.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(int position) {
    }

    @Override
    public void onTabReselected(int position) {
    }

    //ViewPager.OnPageChangeListener接口重载
    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int position) {
        mBottomNavigationBar.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

}
